// pages/forget-password/forget-password.js
import authService from "../../services/auth.service";
import {
  REGEX_PASSWORD
} from "../../utils/regexes";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    username: '',
    phone: '',
    realName: '',
    newPwd: '',
    confirmPwd: '',
    username: '',
    steps: [{
        text: '步骤一',
      },
      {
        text: '步骤二',
      },
      {
        text: '步骤三',
      },
    ],
  },
  async nextStep() {
    if (this.data.active == 0) {
      await this.checkUsername();
    } else if (this.data.active == 1) {
      await this.checkMainInfo();
    } else if (this.data.active == 2) {
      await this.resetPassword();
    }
  },
  async checkUsername() {
    try {
      if (this.data.username == null || this.data.username == "") {
        wx.showToast({
          title: '请输入用户名',
          icon: "error"
        });
        return;
      }

      let res = await authService.checkUsernameBeforeResetPassword(this.data.username);
      if (res.data.data) {
        this.setData({
          active: this.data.active + 1,
        })
      } else {
        wx.showToast({
          title: '用户名不存在',
          icon: "error"
        });
        return;
      }
    } catch (err) {
      wx.showToast({
        title: err.data && err.data.message || err.errMsg,
        icon: 'error'
      });
      throw err;
    } finally {

    }
  },

  async checkMainInfo() {
    try {
      if (this.data.phone == null || this.data.phone == "") {
        wx.showToast({
          title: '请输入手机号',
          icon: "error"
        });
        return;
      }
      if (this.data.realName == null || this.data.realName == "") {
        wx.showToast({
          title: '请输入真实姓名',
          icon: "error"
        });
        return;
      }

      const body = {
        username: this.data.username,
        phone: this.data.phone,
        realName: this.data.realName,
      };
      const res = await authService.checkMainInfoBeforeReset(body);
      if (res.data.data == true) {
        this.setData({
          active: this.data.active + 1,
        })
      } else {
        wx.showToast({
          title: '信息校验不通过',
          icon: "error"
        });
        return;
      }
    } catch (err) {
      wx.showToast({
        title: err.data && err.data.message || err.errMsg,
        icon: 'error'
      });
      throw err;
    } finally {

    }
  },

  async resetPassword() {
    try {
      if (this.data.newPwd == null || this.data.newPwd == "") {
        wx.showToast({
          title: '请输入新密码',
          icon: "error"
        });
        return;
      }
      if (!REGEX_PASSWORD.test(this.data.newPwd)) {
        wx.showToast({
          title: '密码格式应为8-18位数字、字母、符号的任意两种组合',
          icon: "error"
        });
        return;
      }
      if (this.data.confirmPwd == null || this.data.confirmPwd == "") {
        wx.showToast({
          title: '请输入确认密码',
          icon: "error"
        });
        return;
      }
      if (this.data.newPwd != this.data.confirmPwd) {
        wx.showToast({
          title: '两次密码输入不一致',
          icon: "error"
        });
        return;
      }

      const body = {
        username: this.data.username,
        phone: this.data.phone,
        realName: this.data.realName,
        newPwd: this.data.newPwd,
        confirmPwd: this.data.confirmPwd,
      };
      await authService.resetPassword(body);
      wx.showToast({
        title: '密码重置成功，请重新登录',
        icon: "success"
      });
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } catch (err) {
      wx.showToast({
        title: err.data && err.data.message || err.errMsg,
        icon: 'error'
      });
      throw err;
    } finally {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      username: options.username,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})